<!--
 * @Author: your name
 * @Date: 2021-08-17 11:06:01
 * @LastEditTime: 2021-08-18 11:27:07
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \golte-starlife-admin2\src\components\staffAdsmallster\newly.vue
-->
<!--新增员工-->
<template>
  <div>
    <div class="hander">新增/编辑员工</div>
    <div class="entirety">
      <search-staff-back></search-staff-back>
      <new-base-info
        @save="saveBaseInfo"
        :staffBasicVo1="staffBasicVo1"
      ></new-base-info>
      <new-staff-status
        @save="saveStaffStatus"
        :staffBasicVo1="staffBasicVo1"
      ></new-staff-status>
      <!-- <arrange-work></arrange-work> -->
      <arrange-work-1
        @save="saveArrangeWork"
        :staffBasicVo1="staffBasicVo1"
      ></arrange-work-1>
      <id-card-and-bank-card
        @save="saveIdAndBank"
        :staffBasicVo1="staffBasicVo1"
        :staffAccountVo1="staffAccountVo1"
      ></id-card-and-bank-card>
      <staff-skills
        @save="saveSkills"
        :staffSkillList1="staffSkillList1"
      ></staff-skills>
      <apprentice-info
        @save="saveApprentceInfo"
        :teacherPupil1="teacherPupil1"
      ></apprentice-info>
      <work-life
        @save="saveStaffWork"
        :staffWorkExpList1="staffWorkExpList1"
      ></work-life>
      <staff-back :staffBackCheck1="staffBackCheck1"></staff-back>
    </div>
    <div class="buttons">
      <el-button style="background-color:#fd804c;color: #fff;" @click="goBack"
        >返回</el-button
      >
      <el-button
        style="background-color:#fd804c;color: #fff;margin-left:40px;"
        @click="Save"
        :loading="isSaveLoading"
        >保存</el-button
      >
    </div>
  </div>
</template>

<script>
import searchBack from "./searchStaffBack.vue";
import ApprenticeInfo from "./apprenticeInfo.vue";
import ArrangeWork from "./arrangeWork.vue";
import IdCardAndBankCard from "./idCardAndBankCard.vue";
import newBaseInfo from "./newBaseInfo.vue";
import NewStaffStatus from "./newStaffStatus.vue";
import StaffBack from "./staffBack.vue";
import StaffSkills from "./staffSkills.vue";
import WorkLife from "./workLife.vue";
import { verifyPhone } from "./utils";
import ArrangeWork1 from "./arrangeWork1.vue";
import moment from "moment";
import SearchStaffBack from "./searchStaffBack.vue";
export default {
  inject: ["reload"],
  data() {
    return {
      isSaveLoading: false,
      titleTip: "",
      staffBasicVo: {},
      workStatus: {},
      idCardAndBank: {},
      skills: [
        {
          skillType: "",
          skillCodeList: []
        }
      ],
      ApprentceInfo: [],
      staffWork: [
        {
          experience: ""
        }
      ],
      staffArrangeWork: {
        restDay: "",
        leaveStartTime: "",
        leaveEndTime: "",
        staffCode: ""
      }, // 排班查询条件
      // 查询员工信息
      staffBasicVo1: {}, //员工基础信息
      staffAccountVo1: {}, // 银行信息
      staffSkillList1: [], // 技能信息
      teacherPupil1: {}, // 师徒信息
      staffWorkExpList1: [], // 工作经历信息
      staffBackCheck1: {} // 背调征信信息
    };
  },
  computed: {
    saveText() {
      if (this.isSaveLoading === false) {
        return "保存";
      } else {
        return "保存中";
      }
    }
  },
  components: {
    newBaseInfo,
    NewStaffStatus,
    ArrangeWork,
    ArrangeWork1,
    IdCardAndBankCard,
    StaffSkills,
    ApprenticeInfo,
    WorkLife,
    StaffBack,
    SearchStaffBack
  },
  methods: {
    validateTotal() {
      // 全局必填数据校验
      let flag = true;
      const staffBasicVo = this.staffBasicVo; // 头部基础信息
      const idCardAndBank = this.idCardAndBank; // 身份证和银行卡信息
      const staffWork = this.staffWork; // 工作经历信息
      // 校验排班请假时间
      if (
        this.staffArrangeWork.leaveStartTime &&
        this.staffArrangeWork.leaveEndTime
      ) {
        const start = moment(
          this.staffArrangeWork.leaveStartTime,
          "YYYY-MM-DD HH:mm"
        );
        const end = moment(
          this.staffArrangeWork.leaveEndTime,
          "YYYY-MM-DD HH:mm"
        );
        const diff1 = moment(end).diff(moment(start), "minutes");
        if (diff1 <= 0) {
          this.$message.error("请假结束时间必须大于请假开始时间");
          flag = false;
          return false;
        }
        // 校验排班请假时间必须在8：00 和 21：00之前
        const startHour = moment(
          this.staffArrangeWork.leaveStartTime,
          "YYYY-MM-DD HH:mm"
        ).hours();
        const endHour = moment(
          this.staffArrangeWork.leaveEndTime,
          "YYYY-MM-DD HH:mm"
        ).hours();
        //console.log(startHour, endHour);
        if (startHour < 8 || startHour > 21) {
          this.$message.error("请假开始时间必须在8点至21点之间");
          flag = false;
          return false;
        }
        if (endHour < 8 || endHour > 21) {
          this.$message.error("请假结束时间必须在8点至21点之间");
          flag = false;
          return false;
        }
      }
      if (
        this.staffArrangeWork.leaveStartTime &&
        !this.staffArrangeWork.leaveEndTime
      ) {
        this.$message.error("请填写请假结束时间");
        flag = false;
        return false;
      }
      if (
        !this.staffArrangeWork.leaveStartTime &&
        this.staffArrangeWork.leaveEndTime
      ) {
        this.$message.error("请填写请假开始时间");
        flag = false;
        return false;
      }
      if (!staffBasicVo.avatarAddress) {
        this.$message.error("请上传头像图片");
        flag = false;
        return false;
      }
      if (!staffBasicVo.realName) {
        this.$message.error("请填写员工姓名");
        flag = false;
        return false;
      }
      if (!staffBasicVo.phone) {
        this.$message.error("请填写员工手机号");
        flag = false;
        return false;
      }
      if (staffBasicVo.phone && !verifyPhone(staffBasicVo.phone)) {
        this.$message.error("请填写正确的员工手机号");
        flag = false;
        return false;
      }
      if (!staffBasicVo.entryTime) {
        this.$message.error("请选择员工入职时间");
        flag = false;
        return false;
      }
      if (!staffBasicVo.cityCode || !staffBasicVo.businessAreaCode) {
        this.$message.error("请选择完整员工归属");
        flag = false;
        return false;
      }
      // 校验银行卡信息
      if (!idCardAndBank.cardHolderName) {
        this.$message.error("请填写持卡人姓名");
        flag = false;
        return false;
      }
      if (!idCardAndBank.bankReservePhone) {
        this.$message.error("请填写银行预留手机号");
        flag = false;
        return false;
      }
      if (
        idCardAndBank.bankReservePhone &&
        !verifyPhone(idCardAndBank.bankReservePhone)
      ) {
        this.$message.error("请填写正确的银行预留手机号");
        flag = false;
        return false;
      }
      if (!idCardAndBank.bankCode) {
        this.$message.error("请选择所属银行");
        flag = false;
        return false;
      }
      if (!idCardAndBank.openBankAcctName) {
        this.$message.error("请选择支行名称");
        flag = false;
        return false;
      }
      if (!idCardAndBank.staffCard) {
        this.$message.error("请填写身份证号码");
        flag = false;
        return false;
      }
      if (idCardAndBank.staffCard && idCardAndBank.staffCard.length != 18) {
        this.$message.error("身份证号码必须为18位");
        flag = false;
        return false;
      }
      if (!idCardAndBank.bankCardNo) {
        this.$message.error("请填写银行卡号");
        flag = false;
        return false;
      }
      // 校验工作经历多个不能含空   单个可以含空
      if (staffWork.length > 1) {
        staffWork.forEach(item => {
          if (item.experience === "") {
            flag = false;
          }
        });
        if (!flag) {
          this.$message.error("请将工作经历填写完整");
          return false;
        }
      }
      // 排班字段校验
      // 休息日为必传字段
      if (!this.staffArrangeWork.restDay) {
        this.$message.error("请填写员工休息日");
        flag = false;
        return false;
      }
      // 技能模块校验
      if (!this.skills[0].skillType) {
        this.$message.error("请选择员工主要技能类别");
        flag = false;
        return false;
      }
      if (this.skills[0].skillCodeList.length === 0) {
        this.$message.error("请选择员工技能");
        flag = false;
        return false;
      }
      return flag;
    },
    saveBaseInfo(value) {
      this.staffBasicVo = value;
    },
    saveStaffStatus(value) {
      this.workStatus = value;
    },
    saveIdAndBank(value) {
      this.idCardAndBank = value;
    },
    saveSkills(value) {
      this.skills = value;
    },
    saveApprentceInfo(value) {
      this.ApprentceInfo = value;
    },
    saveStaffWork(value) {
      this.staffWork = value;
    },
    saveArrangeWork(value) {
      this.staffArrangeWork = value;
    },
    async Save() {
      //新增编辑保存
      const flag = this.validateTotal();
      if (!flag) return;
      if (this.isSaveLoading === true) {
        this.$message.error("正在保存中，请耐心等待");
        return false;
      }
      const {
        bankCardNo,
        bankCode,
        bankName,
        bankReservePhone,
        cardHolderName,
        openBankAcctName,
        staffCard
      } = this.idCardAndBank;
      const staffAccountVo = {
        // 账户信息
        bankCardNo,
        bankCode,
        bankName,
        bankReservePhone,
        cardHolderName,
        openBankAcctName,
        staffCard
      };
      const {
        avatarAddress,
        businessAreaCode,
        cityCode,
        entryTime,
        isCrossBusinessArea,
        level,
        phone,
        praiseRate,
        realName,
        workYears,
        businessAreaName
      } = this.staffBasicVo;
      const { staffCardPos, staffCardBack } = this.idCardAndBank;
      const { workStartHours, workEndHours, workStatus } = this.workStatus;
      const staffBasicVo = {
        // 员工基础信息
        avatarAddress,
        businessAreaCode,
        businessAreaName,
        cityCode,
        entryTime,
        isCrossBusinessArea,
        level,
        phone,
        praiseRate,
        realName,
        staffCardPos,
        staffCardBack,
        workStartHours,
        workEndHours,
        workStatus,
        workYears
      };
      const data = {
        staffAccountVo, // 员工账户信息
        staffBasicVo, //员工基础信息
        staffCode: this.$route.query.staffCode
          ? this.$route.query.staffCode
          : "",
        staffSkillList: this.skills, // 选中技能列表
        staffTeacherPupilList: this.ApprentceInfo, // 徒弟信息
        staffWorkExpList: this.staffWork, // 员工经历
        staffSchedulingVo: this.staffArrangeWork, // 员工排班查询信息
        staffBackInquireVo: {
          // 员工征信信息
          creditInvestigationRecord: "0",
          illegalCrime: "0",
          legalDisputes: "0",
          backToneResult: "0",
          backUrl: "www.baidu1.com"
        },
        staffLoginVo: {
          // 员工登录信息
          nickname: realName, // 姓名
          userName: phone // 手机号
        },
        supplierCode: "00001" // 供应商code
      };
      //console.log(data);
      this.isSaveLoading = true;
      await this.$http({
        url: "/staffadmin/homeService/staffManager/addOrUpdateStaff",
        method: "POST",
        data
      }).then(res => {
        this.isSaveLoading = false;
        this.$message.success("保存成功");
        this.reload();
        // 新增跳转员工列表页
        if (this.$route.query.staffCode) {
          this.reload();
        } else {
          this.$router.push({
            path: "/stafflist"
          });
          this.reload();
        }
      });
    },
    async fetchStaffDetail() {
      // 查询员工详情
      const staffCode = this.$route.query.staffCode;
      if (staffCode) {
        this.$http({
          url: "/staffadmin/homeService/staffManager/fetchStaffDetail",
          method: "get",
          params: {
            staffCode
          }
        }).then(data => {
          if (data) {
            const {
              staffBasicVo, //员工基础信息
              staffAccountVo, // 银行信息
              staffSkillList, // 技能信息
              teacherPupil, // 师徒信息
              staffWorkExpList, // 工作经历信息
              staffBackCheck // 背调征信信息
            } = data;
            this.staffBasicVo1 = staffBasicVo;
            this.staffAccountVo1 = staffAccountVo;
            this.staffSkillList1 = staffSkillList;
            this.teacherPupil1 = teacherPupil;
            this.staffWorkExpList1 = staffWorkExpList;
            this.staffBackCheck1 = staffBackCheck;
          }
        });
      }
    },
    goBack() {
      // 回退至员工列表
      this.$router.push({
        path: "/stafflist"
      });
    }
  },
  activated() {
    // 编辑进入查询当前员工
    this.fetchStaffDetail();
  }
};
</script>

<style lang="scss" scoped>
.hander {
  width: 100%;
  height: 50px;
  line-height: 50px;
  font-size: 20px;
  font-weight: 900;
  margin-left: 10px;
}
.entirety {
  margin: 10px;
  border: 10px solid #ebeef1;
  background-color: #ebeef1;
  border-radius: 10px;
}
.buttons {
  text-align: center;
}
</style>
